/*
 * Created by JFormDesigner on Thu Apr 29 15:45:50 CST 2021
 */

package com.aivin.firplugin.view;

import com.aivin.firplugin.bean.ApkInfo;
import com.aivin.firplugin.bean.AppDetail;
import com.aivin.firplugin.httplib.HttpRespondCallback;
import com.aivin.firplugin.httplib.NetTool;
import com.aivin.firplugin.httplib.ProgressListener;
import com.aivin.firplugin.myconfig.IpConfig;
import com.aivin.firplugin.tool.ApkTool;
import com.aivin.firplugin.tool.WkLog;
import com.alibaba.fastjson.JSON;
import net.miginfocom.swing.MigLayout;
import org.apache.http.message.BasicNameValuePair;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * @author aivin
 */
public class ApkUpload {
    private String bundleID ;
    private String api_token ;
    private String updateLog ;
    public ApkUpload( String apkPath ,String bundleID ,String api_token ,String updateLog) {
        this.bundleID = bundleID ;
        this.api_token = api_token ;
        this.updateLog = updateLog ;
        initComponents();
        getApkInfo(apkPath) ;
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        apkuploadPanel = new JPanel();
        label5 = new JLabel();
        appName = new JLabel();
        label6 = new JLabel();
        appLogo = new JLabel();
        label3 = new JLabel();
        bundleId = new JLabel();
        label1 = new JLabel();
        versionName = new JLabel();
        label2 = new JLabel();
        versionCode = new JLabel();
        label4 = new JLabel();
        uploadProgress = new JLabel();
        label7 = new JLabel();
        iconUploadProgress = new JLabel();
        uploadApkBtn = new JButton();
        errorShow = new JLabel();

        //======== apkuploadPanel ========
        {
            apkuploadPanel.setLayout(new MigLayout(
                "hidemode 3",
                // columns
                "[66,fill]" +
                "[509,fill]",
                // rows
                "[]" +
                "[]" +
                "[]" +
                "[26]" +
                "[26]" +
                "[43]" +
                "[]" +
                "[50]" +
                "[92]"));

            //---- label5 ----
            label5.setText("App\u540d");
            apkuploadPanel.add(label5, "cell 0 0");

            //---- appName ----
            appName.setText("-");
            apkuploadPanel.add(appName, "cell 1 0");

            //---- label6 ----
            label6.setText("Logo");
            apkuploadPanel.add(label6, "cell 0 1");

            //---- appLogo ----
            appLogo.setText("-");
            apkuploadPanel.add(appLogo, "cell 1 1");

            //---- label3 ----
            label3.setText("\u5305\u540d");
            apkuploadPanel.add(label3, "cell 0 2");

            //---- bundleId ----
            bundleId.setText("-");
            apkuploadPanel.add(bundleId, "cell 1 2");

            //---- label1 ----
            label1.setText("\u7248\u672c\u540d");
            apkuploadPanel.add(label1, "cell 0 3");

            //---- versionName ----
            versionName.setText("-");
            apkuploadPanel.add(versionName, "cell 1 3");

            //---- label2 ----
            label2.setText("\u7248\u672c\u53f7");
            apkuploadPanel.add(label2, "cell 0 4");

            //---- versionCode ----
            versionCode.setText("-");
            apkuploadPanel.add(versionCode, "cell 1 4");

            //---- label4 ----
            label4.setText("Apk\u8fdb\u5ea6");
            apkuploadPanel.add(label4, "cell 0 5");

            //---- uploadProgress ----
            uploadProgress.setText("-");
            apkuploadPanel.add(uploadProgress, "cell 1 5,alignx center,growx 0");

            //---- label7 ----
            label7.setText("ico\u8fdb\u5ea6");
            apkuploadPanel.add(label7, "cell 0 6");

            //---- iconUploadProgress ----
            iconUploadProgress.setText("-");
            apkuploadPanel.add(iconUploadProgress, "cell 1 6,alignx center,growx 0");

            //---- uploadApkBtn ----
            uploadApkBtn.setText("\u4e0a\u4f20");
            apkuploadPanel.add(uploadApkBtn, "cell 1 7,growy");

            //---- errorShow ----
            errorShow.setText("-");
            errorShow.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
            errorShow.setOpaque(true);
            apkuploadPanel.add(errorShow, "cell 1 8,growy");
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents



        uploadApkBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

                // bundleID
                boolean isTheSameApp = bundleID.equals(apkInfo.getBundleId());
                if(!isTheSameApp){
                    StringBuilder sb = new StringBuilder() ;
                    sb.append("<html><font color =red>") ;
                    sb.append("apk文件跟所选项目不是同一个，请正确选择") ;
                    sb.append("<br> ") ;
                    sb.append(bundleID) ;
                    sb.append("<br> ") ;
                    sb.append(apkInfo.getBundleId()) ;
                    sb.append("</font></html>\r\n") ;
                    errorShow.setText(sb.toString());
                    return;
                }
                getUploadToken(bundleID ,  api_token);
            }
        });
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel apkuploadPanel;
    private JLabel label5;
    private JLabel appName;
    private JLabel label6;
    private JLabel appLogo;
    private JLabel label3;
    private JLabel bundleId;
    private JLabel label1;
    private JLabel versionName;
    private JLabel label2;
    private JLabel versionCode;
    private JLabel label4;
    private JLabel uploadProgress;
    private JLabel label7;
    private JLabel iconUploadProgress;
    private JButton uploadApkBtn;
    private JLabel errorShow;
    // JFormDesigner - End of variables declaration  //GEN-END:variables


    private void getUploadToken(String bundleID  ,String api_token ){
        ArrayList<BasicNameValuePair> parameters = new ArrayList<>();
        parameters.add(new BasicNameValuePair("type", "android"));
        parameters.add(new BasicNameValuePair("bundle_id", bundleID));
        parameters.add(new BasicNameValuePair("api_token", api_token ));
        new NetTool().postMsg(IpConfig.API_GETUPLOAD_TOKEN  ,parameters ,httpRespondCallback);
    }

    private HttpRespondCallback httpRespondCallback = new HttpRespondCallback() {
        @Override
        public void onGetInfo(String msg, boolean hasError) {
            WkLog.showMsg(hasError+"  "+msg);

            if(hasError){
                WkLog.showMsg("app上传凭证 访问异常");
                return;
            }

            AppDetail appDetail = JSON.parseObject(msg, AppDetail.class);

            new NetTool().postApkFile( appDetail ,apkInfo ,updateLog ,progressListenerApk);
            new NetTool().postApkIcon( appDetail ,apkInfo  ,progressListenerIcon);

        }
    };

    private ProgressListener progressListenerApk = new ProgressListener() {
        @Override
        public void transferProgress(String num) {
            uploadProgress.setText( num);
        }

    } ;

    private ProgressListener progressListenerIcon = new ProgressListener() {
        @Override
        public void transferProgress(String num) {
            iconUploadProgress.setText( num);
        }

    } ;

    private ApkInfo apkInfo ;
    private void getApkInfo(String apkPath){
        apkInfo = new ApkTool().parseApk(apkPath);
        appName.setText(apkInfo.getName());
        appLogo.setText(apkInfo.getIcon());
        versionName.setText(apkInfo.getVersionName());
        versionCode.setText(apkInfo.getVersionCode());
        bundleId.setText(apkInfo.getBundleId());

        String iconpath = new ApkTool().queryIconPath( apkPath ,apkInfo.getIcon() );
        Image image = Toolkit.getDefaultToolkit().getImage(iconpath);
        ImageIcon icon_ = new ImageIcon(image);
        icon_.setImage(icon_.getImage().getScaledInstance(70, 70, Image.SCALE_AREA_AVERAGING));
        appLogo.setIcon(icon_);
        apkInfo.setIconLocalPath(iconpath);
    }




    public JPanel getApkuploadPanel() {
        return apkuploadPanel;
    }
}
